﻿using Antlr3.ST;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace Jhecis.Service
{
    public class SendHelper
    {
        public SendHelper() 
        {
            StGroup = new StringTemplateGroup("main", AppDomain.CurrentDomain.BaseDirectory + "\\Templates");
        }
        public StringTemplateGroup StGroup { get; private set; }
        BingLiBLL bll = new BingLiBLL();
        /// <summary>
        /// 单个人发送xml上报病历
        /// </summary>
        /// <param name="Pvid"></param>
        public void SendXmlByPvid(string Pvid,string DirName="")
        {
            try
            {
                #region 上传单个人的病历xml  
               
                List<JZ_BL_Model> models = bll.GetBLModelByPvid(Pvid);
                for (int i = 0; i < models.Count; i++)
                {
                    JZ_BL_Model model = models[i];
                    #region 所有属性默认值为 "0"
                    //var properties = typeof(JZ_BL_Model).GetProperties();
                    //foreach (System.Reflection.PropertyInfo info in properties)
                    //{
                    //    string Getval = (string)model.GetType().GetProperty(info.Name).GetValue(model, null);
                    //    if (string.IsNullOrEmpty(Getval))
                    //    {
                    //        model.GetType().GetProperty(info.Name).SetValue(model, "0", null);
                    //    }
                    //}
                    #endregion
                    #region 解析
                    //子xml加载
                    StringTemplate st = StGroup.GetInstanceOf("JizhenBingLi");
                    //子xml赋值
                    st.SetAttribute("DataContext", model);
                    string blstr = st.ToString();
                    byte[] b = System.Text.Encoding.UTF8.GetBytes(blstr);
                    string ChildBase64 = Convert.ToBase64String(b);
                    //主xml加载BODY
                    model.BODY = ChildBase64;
                    StringTemplate stCDR = StGroup.GetInstanceOf("CDRParm");
                    //主xml处理
                    stCDR.SetAttribute("DataContext", model);
                    #endregion
                    string res=stCDR.ToString();
                    #region 调用WebServers,注册CDA文档
                    LogHelper.Instance.WriteInfo("上传开始"+ model.P_PATIENTNAME + "【"+ model.P_PATIENTID + "】");
                    LogHelper.Instance.WriteInfo("上传文档["+model.P_PATIENTID+"]id:" + model.P_GUID);
                    LogHelper.Instance.WriteXml(res, model.P_PATIENTID+"()"+ model.P_GUID+".xml", DirName);
                    string s = CDRHelper.SendXml(res);
                    #endregion
                }
                #endregion
            }
            catch (Exception ex)
            {
                LogHelper.Instance.WriteError("单个人员上传出错["+ Pvid + "] :" + ex.Message);
            }
        }
    }
}
